//医生
import {departmentlist,drugsDrugstype,WorkNuserrReq, WorkDoctorReq, ListReqSearch, ListReq, NursetReqSearch, getDoctorReq, RosteringDoctorReq } from '@/api/nursecontrollerApi/nursecontroller.qer';
//进出库
import { warehouseSearchReq, drugsAddReq, changeReq, delReq, allDelReq } from '@/api/warehouseApi/warehouse.api'
//
import qs from 'qs'
import dayjs from 'dayjs';
export default {
    //蹇江渝私有
    namespaced: true,
    state: {
        list: {},
        //医生可查找列表
        lists: {},
        //医生排班
        doctorDay: {
            head: '/1667806938017.webp',
            date: [],
            department: '',
            name: ''
        },

        //药品接口
        warehouseList: {},
        //护士可查找接口
        nurseList: {},
        //医生排班
        nurseDay: {
            head: '/1667809973538.webp',
            date: [],
            department: '',
            name: ''
        },
        //医生个人信息
        doctorData: {},
        nurseData:{},
        //药品列表
        drugsDrugstypeList:[],
        //科室列表
        departmentList:[]
    },
    //修改数据
    mutations: {
        M_changeList(state: any, payload: {}) {
            state.list = payload
        },
        M_changeLists(state: any, payload: {}) {
            state.lists = payload
        },
        //医生排班
        M_doctorDay(state: any, payload: {}) {

            // payload.date=JSON.parse(payload.date)
            // console.log(payload,'~~~~~~~~~~~~~~~~~~~~~~~');
            state.doctorDay = payload
        },
        //护士
        M_changenurseList(state: any, payload: {}) {
            state.nurseList = payload
        },
        //进出库
        M_warehouseList(state: any, payload: {}) {
            state.warehouseList = payload
        },
        //医生个人资料
        M_doctor(state: any, payload: {}) {
            state.doctorData = payload
        },
        M_nurse(state: any, payload: {}) {
            state.nurseData = payload
        },
        //所有药品
        M_drugsDrugstype(state: any, payload: []){
            state.drugsDrugstypeList=payload
        },
        //所有科室
        M_department(state: any, payload: []){
            state.departmentList=payload
        }

    },
    //做异步处理的
    actions: {
        //获取统计列表
        async A_list({ commit }, payload: any) {
            let res = await ListReqSearch(payload)
            commit('M_changeList', res.data)
        },
        // 获取排班管理
        async  A_liste({ commit }, payload: any) {
            let res = await ListReq(payload)
            commit('M_changeLists', res.data)
        },
        //获取进出库列表
        async A_warehouseList({ commit }, payload: any) {
            let res = await warehouseSearchReq(payload)
            if (res.data.data.length > 0) {
                res.data.data.forEach(i => {
                    i.active = 0
                    i.endtime = dayjs(i.endtime).format('YYYY-MM-DD ')
                    i.starttime = dayjs(i.starttime).format('YYYY-MM-DD ')
                })
            }
            console.log(res.data.data);
            commit('M_warehouseList', res.data)
        },
        //排班  获取排班 
        async A_getDoctor({ commit }, payload: any) {
            let res = await getDoctorReq(payload)
            // console.log(res.data.data[0].date);
           if(res.data.data[0].date.indexOf('%')==-1){
            res.data.data[0].date=JSON.parse(res.data.data[0].date)
            commit('M_doctorDay', res.data.data[0])
           }else{
            let Arr = res.data.data[0]
            // console.log(Object.values(qs.parse(res.data.data[0].date)));
            Arr.date=Object.values(qs.parse(res.data.data[0].date))
            commit('M_doctorDay', Arr)
           }
           
     
            // let arr = JSON.parse(JSON.stringify(Object.values(qs.parse(res.data.data[0].date))[0]))
            // console.log(arr);
            // let bbb = arr.replaceAll('},', `}**`)
            // let ccc = bbb.split('**')
            // console.log(ccc);
            // let ddd = ccc.map(i => JSON.parse(i))
            // console.log(ddd);

            // Arr.date = ddd
            // console.log(Object.keys(qs.parse(res.data.data[0].date))[0]);

            // let arr = Object.keys(qs.parse(res.data.data[0].date))[0].split('},')
            // console.log(arr);
            // // 内容存仓库
            
        },

        // 修改排班
        async A_rosteringDoctor({ commit }, payload: any) {
            console.log(payload,);

            let res = await RosteringDoctorReq(payload)
            console.log(res);

        },

        // 修改医生信息
        async A_workDoctor({ commit }, payload: any) {
            console.log(payload);
            let res = await WorkDoctorReq(payload)
            console.log(res);
        },
        ////////////////护士
        async A_nurseList({ commit }, payload: any) {
            let res = await NursetReqSearch(payload)
            console.log(res);

            commit('M_changenurseList', res.data)
        },
        async A_rosteringNurse({ commit }, payload: any) {
            console.log(payload);
            let res = await WorkNuserrReq(payload)
            console.log(res);
        },
        //上传药品
        async A_drugsAddReq({ commit }, payload: any) {
            let res = await drugsAddReq(payload)
            return res.data.code
        },
        //修改商品
        async A_change({ commit }, payload: any) {
            console.log(payload);

            let res = await changeReq(payload)
        },
        //删除
        async A_del({ commit }, payload: any) {
            console.log(payload);

            let res = await delReq({ id: payload })
            console.log(res);
        },
        //批量删除
        async A_allDel({ commit }, payload: any) {
            console.log(JSON.stringify(payload));

            let res = await allDelReq({ ids: JSON.stringify(payload) })
            console.log(res);
        },
        //获取科室列表
        async A_departmentlist({commit}){
            let res=await drugsDrugstype()
            console.log(res.data.data);
            commit('M_drugsDrugstype',res.data.data)
        },
        //获取医疗用品
        async A_drugsDrugstype({commit}){
            let res=await departmentlist()
            console.log(res.data.data);
            commit('M_department',res.data.data)
        }
        
    },
    //store的计算属性   
    getters: {},
}